home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1992 June: ROMin Holiday / ADC Developer CD (1992-06) (''ROMin Holiday'')_iso / Developer Connection - 06-1992.iso / Developer Essentials / DTS Sample Code / System 7.0 Samples / CShell⁄THINK C / File2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-02-20  |  4.8 KB  |  219 lines  |  [TEXT/MPS ]

  1. /*
  2. ** Apple Macintosh Developer Technical Support
  3. **
  4. ** Program:     CShell
  5. ** File:        file2.c
  6. ** Written by:  Eric Soldan
  7. **
  8. ** Copyright © 1990-1991 Apple Computer, Inc.
  9. ** All rights reserved.
  10. */
  11.  
  12.  
  13.  
  14. /*****************************************************************************/
  15.  
  16.  
  17.  
  18. #include "CShell.h"                /* Get the CShell includes/typedefs, etc.    */
  19. #include "CShellCommon.h"        /* Get the stuff in common with rez.        */
  20. #include "CShell.protos"        /* Get the prototypes for CShell.            */
  21.  
  22. #ifndef __ERRORS__
  23. #include <Errors.h>
  24. #endif
  25.  
  26. #ifndef __UTILITIES__
  27. #include "Utilities.h"
  28. #endif
  29.  
  30.  
  31.  
  32. /*****************************************************************************/
  33. /*****************************************************************************/
  34.  
  35.  
  36.  
  37. /* AppFreeDocument
  38. **
  39. ** Frees up any application-specific memory in the document.  The document
  40. ** may have a bunch of handles off the main handle of the document.  This
  41. ** is where they are freed.  AppDisposeDocument calls this prior to releasing
  42. ** the ram for the main handle of the document, so release everything else
  43. ** here, or you will have a memory leak.
  44. */
  45.  
  46. #pragma segment File
  47. void    AppFreeDocument(FileRecHndl frHndl)
  48. {
  49.     Handle    textHndl;
  50.  
  51.     if (textHndl = (*frHndl)->doc.textHndl) {
  52.         DisposHandle(textHndl);
  53.         (*frHndl)->doc.textHndl = nil;
  54.     }
  55. }
  56.  
  57.  
  58.  
  59. /*****************************************************************************/
  60.  
  61.  
  62.  
  63. /* AppInitDocument
  64. **
  65. ** Do any additional document initialization here.
  66. */
  67.  
  68. #pragma segment File
  69. OSErr    AppInitDocument(FileRecHndl frHndl)
  70. {
  71.     (*frHndl)->doc.version       = kVersion;
  72.     (*frHndl)->doc.printRecValid = false;
  73.     (*frHndl)->doc.connected     = false;
  74.     (*frHndl)->doc.textHndl      = nil;
  75.  
  76.     return(noErr);
  77. }
  78.  
  79.  
  80.  
  81. /*****************************************************************************/
  82.  
  83.  
  84.  
  85. /* AppReadDocument
  86. **
  87. ** Reads in specified file into the data portion of a document handle.
  88. */
  89.  
  90. #pragma segment File
  91. OSErr    AppReadDocument(FileRecHndl frHndl)
  92. {
  93.     short    fileRefNum;
  94.     OSErr    err;
  95.     char    hstate;
  96.     Ptr        ptr1, ptr2;
  97.     long    count;
  98.     Handle    textHndl;
  99.  
  100.     fileRefNum = (*frHndl)->fileState.refNum;
  101.  
  102.     err = SetFPos(fileRefNum, fsFromStart, 0);
  103.         /* Set the file position to the beginning of the file. */
  104.  
  105.     if (!err) {        /* Read board info from file. */
  106.         hstate = LockHandleHigh((Handle)frHndl);
  107.         ptr1   = (Ptr)&((*frHndl)->doc);
  108.         ptr2   = (Ptr)&((*frHndl)->doc.endFileInfo);
  109.         count  = (long)ptr2 - (long)ptr1;
  110.         err    = FSRead(fileRefNum, &count, ptr1);
  111.         HSetState((Handle)frHndl, hstate);
  112.         if ((*frHndl)->doc.version != kVersion)
  113.             err = kWrongVersion;
  114.     }
  115.  
  116.     if (!err) {        /* Read TextEdit text from file. */
  117.         textHndl = NewHandle(32768);
  118.         if (!(err = MemError())) {
  119.             count = 32768;
  120.                 /* The size of the text isn't saved to disk.  This is the maximum
  121.                 ** that a TextEdit record can accept. */
  122.             hstate = LockHandleHigh(textHndl);
  123.             err    = FSRead(fileRefNum, &count, *textHndl);
  124.             HSetState(textHndl, hstate);
  125.             if (err == eofErr) err = noErr;
  126.             if (err) count = 0;
  127.             SetHandleSize(textHndl, count);
  128.                 /* Set the handle to the actual size of the text on disk. */
  129.             (*frHndl)->doc.textHndl = textHndl;
  130.         }
  131.     }
  132.  
  133.     return(err);
  134. }
  135.  
  136.  
  137.  
  138. /*****************************************************************************/
  139.  
  140.  
  141.  
  142. /* AppWriteDocument
  143. **
  144. ** Writes the data portion of a document handle to the specified file.
  145. */
  146.  
  147. #pragma segment File
  148. OSErr    AppWriteDocument(FileRecHndl frHndl)
  149. {
  150.     short        fileRefNum;
  151.     OSErr        err;
  152.     char        hstate;
  153.     Ptr            ptr1, ptr2;
  154.     long        count, fpos;
  155.     TEHandle    te;
  156.     Handle        textHndl;
  157.  
  158.     fileRefNum = (*frHndl)->fileState.refNum;
  159.  
  160.     err = SetFPos(fileRefNum, fsFromStart, 0);
  161.         /* Set the file position to the beginning of the file. */
  162.  
  163.     if (!err) {        /* Write board info to file. */
  164.         hstate = LockHandleHigh((Handle)frHndl);
  165.         ptr1   = (Ptr)&((*frHndl)->doc);
  166.         ptr2   = (Ptr)&((*frHndl)->doc.endFileInfo);
  167.         count  = (long)ptr2 - (long)ptr1;
  168.         err    = FSWrite(fileRefNum, &count, ptr1);
  169.         HSetState((Handle)frHndl, hstate);
  170.     }
  171.  
  172.     if (!err) {        /* Write out-box TextEdit control text to file. */
  173.         te       = (*frHndl)->doc.outBox;
  174.         textHndl = (*te)->hText;
  175.         count    = (*te)->teLength;
  176.         hstate   = LockHandleHigh(textHndl);
  177.         err      = FSWrite(fileRefNum, &count, *textHndl);
  178.         HSetState(textHndl, hstate);
  179.     }
  180.  
  181.     if (!err) {
  182.         err = GetFPos(fileRefNum, &fpos);
  183.         if (!err) err = SetEOF(fileRefNum, fpos);
  184.     }
  185.  
  186.     return(err);
  187. }
  188.  
  189.  
  190.  
  191. /*****************************************************************************/
  192.  
  193.  
  194.  
  195. #pragma segment File
  196. OSErr    AppDuplicateDocument(FileRecHndl oldFrHndl, FileRecHndl *newFrHndl)
  197. {
  198.     OSErr        err;
  199.     TEHandle    te;
  200.     Handle        oldHndl, newHndl;
  201.     long        size;
  202.  
  203.     err = AppNewDocument(newFrHndl);
  204.     if (!err) {
  205.         te      = (*oldFrHndl)->doc.outBox;
  206.         oldHndl = (*te)->hText;
  207.         size    = (*te)->teLength;
  208.         if (newHndl = NewHandle(size)) {
  209.             (**newFrHndl)->doc.textHndl = newHndl;
  210.             BlockMove(*oldHndl, *newHndl, size);
  211.         }
  212.         else err = memFullErr;
  213.     }
  214.     return(err);
  215. }
  216.  
  217.  
  218.  
  219.